package com.errol.generic.util;

import org.springframework.lang.Nullable;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Errol on 2016/7/11.
 * 日期格式转换工具类
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2020/10/21
 * @since v4.7.6
 */
public class DateFormatUtil {

    public static final String PATTERN_A = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_B = "yyyyMMddHHmmss";
    public static final String PATTERN_C = "yyyy-MM-dd";
    public static final String PATTERN_D = "yyyy年M月d日";
    public static final String PATTERN_E = "M月d日 H时m分";
    public static final String PATTERN_F = "HH:mm:ss";
    public static final String PATTERN_G = "yyyy-MM-dd HH:mm";
    public static final String PATTERN_H = "yyyyMM";
    public static final String PATTERN_I = "yyyy-MM";
    public static final String PATTERN_J = "yyM";
    public static final String PATTERN_K = "yy/MM/dd";
    public static final String PATTERN_L = "yyyyMMdd";
    public static final String PATTERN_M = "M月d日";
    public static final String PATTERN_N = "yyyy/MM/dd HH:mm:ss";
    public static final String PATTERN_O = "yyyy/MM/dd";
    public static final String PATTERN_P = "yyyy年MM月";
    public static final String PATTERN_Q = "MM-dd HH:mm";
    public static final String PATTERN_R = "yyyyMMddHHmmssSSS";
    public static final String PATTERN_S = "HH:mm:ss.SSS";

    @Nullable
    public static String date2String(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        return new SimpleDateFormat(pattern).format(date);
    }

    @Nullable
    public static Date string2Date(String dateStr, String pattern) {
        if ("".equals(dateStr)) {
            return null;
        }
        try {
            return new SimpleDateFormat(pattern).parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Nullable
    public static Date stamp2Date(String timestamp) {
        if ("".equals(timestamp)) {
            return null;
        }
        try {
            return new Date(Long.parseLong(timestamp));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Nullable
    public static String stamp2String(String timestamp, String pattern) {
        if ("".equals(timestamp)) {
            return null;
        }
        return stamp2String(Long.parseLong(timestamp), pattern);
    }

    @Nullable
    public static String stamp2String(long timestamp, String pattern) {
        try {
            return new SimpleDateFormat(pattern).format(new Date(timestamp));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Nullable
    public static Long string2stamp(String dateStr, String pattern) {
        Date date;
        if (dateStr == null || "".equals(dateStr) || (date = string2Date(dateStr, pattern)) == null) {
            return null;
        }
        return date.getTime();
    }
}
